//
//  SetTimeViewController.m
//  MedicineBox
//
//  Created by dllo on 15/8/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SetTimeViewController.h"
#import "SaveTools.h"
#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"
@interface SetTimeViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, retain) NSMutableArray *yearArray;
@property (nonatomic, retain)  NSMutableArray *monthArray;
@property (nonatomic, retain)  NSMutableArray *DaysArray;

@property (nonatomic, retain)  NSMutableArray *hourArray;
@property (nonatomic, retain)  NSMutableArray *minuteArray;

@property (nonatomic, retain)  NSMutableArray *repeatArray;

@property (nonatomic, retain) NSString *dateStr;
@property (nonatomic, retain) NSString *monthStr;
@property (nonatomic, retain) NSString *dayStr;

@property (nonatomic, retain) NSString *hourStr;
@property (nonatomic, retain) NSString *minStr;
@property (nonatomic, retain) NSString *timeStr;
@property (nonatomic, retain) NSString *repeatStr;
@property (nonatomic, retain) UIView *picView;

@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSMutableDictionary *dic;


@end

@implementation SetTimeViewController

-(void)dealloc
{
    [_yearArray release];
    [_monthArray release];
    [_DaysArray release];
    [_dateStr release];
    [_monthStr release];
    [_dayStr release];
    [_picView release];
    [_hourArray release];
    [_minuteArray release];
    [_hourStr release];
    [_minStr release];
    [_timeStr release];
    [_repeatArray release];
    [_repeatStr release];
    [_dic release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dic = [NSMutableDictionary dictionary];
        _yearArray = [[NSMutableArray alloc]init];
        for (int i = 2015; i <= 2050 ; i++)
        {
            [_yearArray addObject:[NSString stringWithFormat:@"%d年",i]];
            
        }
        _monthArray = [[NSMutableArray alloc]init];
        for (int i = 1; i <= 12 ; i++)
        {
            [_monthArray addObject:[NSString stringWithFormat:@"%d月",i]];
            
        }
        _DaysArray = [[NSMutableArray alloc]init];
        for (int i = 1; i <= 31 ; i++)
        {
            [_DaysArray addObject:[NSString stringWithFormat:@"%d日",i]];
            
        }
        _hourArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 24 ; i++)
        {
            
            [_hourArray addObject:[NSString stringWithFormat:@"%d",i]];
            
        }
        _minuteArray = [[NSMutableArray alloc]init];
        for (int i = 0; i <= 59 ; i++)
        {
            [_minuteArray addObject:[NSString stringWithFormat:@"%d",i]];
            
        }
        self.repeatArray = @[@"不重复",@"每天",@"每2天",@"每3天",@"每4天",@"每5 天",@"每6天",@"每星期"].mutableCopy;

        
        }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self createNavigation];
    [self createSubView];
    
}

#pragma mark -
#pragma mark 设置导航栏
- (void)createNavigation
{
    self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.imageView.userInteractionEnabled = YES;
    self.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2.png" ofType:nil]];
    [self.view addSubview:self.imageView];
    [_imageView release];
    
    //标题
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 0.5)];
    [self.imageView addSubview:navView];
    navView .backgroundColor = [UIColor whiteColor];
    navView.nightBackgroundColor = UIColorFromRGB(0x444444);
    [navView release];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100) / 2, 30, 100, 30)];
    titleLabel.text = @"服药提醒";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.nightTextColor = [UIColor whiteColor];
    //    titleLabel.nightBackgroundColor = UIColorFromRGB(0x444444);
    [self.imageView addSubview:titleLabel];
    [titleLabel release];
    
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10 , 30, 40, 30)];
    [self.imageView addSubview:backButton];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton release];
    
    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50 , 30, 40, 30)];
    [self.imageView addSubview:doneButton];
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [doneButton release];

}
- (void)buttonAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//点击完成并返回传值
- (void)doneButtonAction:(id)sender
{
    
    UITextField *people = (UITextField *)[self.view viewWithTag:10];
    [self.dic setObject:people.text forKey:@"people"];
    UITextField *name = (UITextField *)[self.view viewWithTag:20];
    [self.dic setObject:name.text forKey:@"name"];
    UITextField *mark = (UITextField *)[self.view viewWithTag:30];
    [self.dic setObject:mark.text forKey:@"mark"];
    
    [self.dic setObject:self.hourStr forKey:@"hour"];
    [self.dic setObject:self.minStr forKey:@"minute"];
    
    
    // 从文件中存取数据
    
    NSString *path = [SaveTools pathWithName:@"alarm" directory:NSDocumentDirectory];
    NSFileManager *file = [NSFileManager defaultManager];
    if ([file fileExistsAtPath:path]) {
        NSMutableArray *array = [NSMutableArray array];
        array = [NSMutableArray arrayWithContentsOfFile:path];
        [array addObject:self.dic];
        [array writeToFile:path atomically:path];
    }
    else {
        NSMutableArray *array = [NSMutableArray array];
        [array addObject:self.dic];
        [SaveTools saveWithName:@"alarm" directory:NSDocumentDirectory value:array];
    }
    
    [self.delegate createTable];
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 
#pragma mark 创建view及点击事件
- (void)createSubView
{
    UIImageView *people = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 30, 30)];
    people.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"people" ofType:@"png"]];
    people.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"people" ofType:@"png"]];
    [self.imageView addSubview:people];
    [people release];
    
    UITextField *peopleField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100,self.view.frame.size.width - 100, 30)];
    [self.imageView addSubview:peopleField];
    peopleField.text = @"我";
    peopleField.placeholder = @"服药者称呼";
    peopleField.layer.borderWidth = 1;
    peopleField.layer.cornerRadius = 5;
    peopleField.layer.borderColor = [UIColor grayColor].CGColor;
    peopleField.delegate = self;
    peopleField.tag = 10;
    [peopleField release];
    
    UIImageView *drug = [[UIImageView alloc] initWithFrame:CGRectMake(10, 140, 30, 30)];
    drug.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drugName" ofType:@"png"]];
    drug.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drugName" ofType:@"png"]];
    [self.imageView addSubview:drug];
    [drug release];
    
    UITextField *drugField = [[UITextField alloc] initWithFrame:CGRectMake(50, 140,self.view.frame.size.width - 100, 30)];
    [self.imageView addSubview:drugField];
    drugField.placeholder = @"药品名称";
    drugField.layer.borderWidth = 1;
    drugField.layer.cornerRadius = 5;
    drugField.layer.borderColor = [UIColor grayColor].CGColor;
    drugField.delegate = self;
    drugField.tag = 20;
    [drugField release];
    
    UIImageView *mark = [[UIImageView alloc] initWithFrame:CGRectMake(10, 180, 30, 30)];
    mark.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"remarks" ofType:@"png"]];
    mark.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"remarks" ofType:@"png"]];
    [self.view addSubview:mark];
    [mark release];
    
    UITextField *markField = [[UITextField alloc] initWithFrame:CGRectMake(50, 180,self.view.frame.size.width - 100, 30)];
    [self.imageView addSubview:markField];
    markField.placeholder = @"输入备注";
    markField.layer.borderWidth = 1;
    markField.layer.cornerRadius = 5;
    markField.layer.borderColor = [UIColor grayColor].CGColor;
    markField.delegate = self;
    markField.tag = 30;
    [markField release];
    
    UIImageView *repeat = [[UIImageView alloc] initWithFrame:CGRectMake(10, 220, 30, 30)];
    repeat.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"repeat" ofType:@"png"]];
    repeat.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"repeat" ofType:@"png"]];
    [self.imageView addSubview:repeat];
    [repeat release];
    
    UILabel *repeatLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 220,self.view.frame.size.width - 100, 30)];
    [self.imageView addSubview:repeatLabel];
    repeatLabel.tag = 100;
    repeatLabel.text = @"点击设置重复时间";
    repeatLabel.nightTextColor = [UIColor whiteColor];
    repeatLabel.textAlignment = NSTextAlignmentCenter;
    repeatLabel.layer.borderWidth = 1;
    repeatLabel.layer.cornerRadius = 5;
    repeatLabel.layer.borderColor = [UIColor grayColor].CGColor;
    repeatLabel.userInteractionEnabled = YES;
    [repeatLabel release];
    
    UITapGestureRecognizer *repTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(repTapAction:)];
    [repeatLabel addGestureRecognizer:repTap];
    [repTap release];
    
    UIImageView *time = [[UIImageView alloc] initWithFrame:CGRectMake(10, 260, 30, 30)];
    time.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"setTime" ofType:@"png"]];
    time.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"setTime" ofType:@"png"]];
    [self.imageView addSubview:time];
    [time release];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 260,self.view.frame.size.width - 100, 30)];
    [self.imageView addSubview:timeLabel];
    timeLabel.tag = 200;
    timeLabel.layer.borderWidth = 1;
    timeLabel.layer.cornerRadius = 5;
    timeLabel.layer.borderColor = [UIColor grayColor].CGColor;
    timeLabel.text = @"点击设置时间(必填)";
    timeLabel.nightTextColor = [UIColor whiteColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.userInteractionEnabled = YES;
    [timeLabel release];
    
    
    UITapGestureRecognizer *timeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeTapAction:)];
    [timeLabel addGestureRecognizer:timeTap];
    [timeTap release];
    
    UIImageView *date = [[UIImageView alloc] initWithFrame:CGRectMake(10, 300, 30, 30)];
    date.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"date" ofType:@"png"]];
    date.nightImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"date" ofType:@"png"]];
    [self.imageView addSubview:date];
    [date release];
    
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 300,self.view.frame.size.width - 100, 30)];
    [self.imageView addSubview:dateLabel];
    dateLabel.tag = 300;
    dateLabel.text = @"点击设置开始提醒";
    dateLabel.nightTextColor = [UIColor whiteColor];
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.layer.borderWidth = 1;
    dateLabel.layer.cornerRadius = 5;
    dateLabel.layer.borderColor = [UIColor grayColor].CGColor;
    dateLabel.userInteractionEnabled = YES;
    [dateLabel release];
    
    UITapGestureRecognizer *dateTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dateTapAction:)];
    [dateLabel addGestureRecognizer:dateTap];
    [dateTap release];
}
//重复点击事件
- (void)repTapAction:(UITapGestureRecognizer *)tap
{
    [self createPickerView:1];
}
//时间设置
- (void)timeTapAction:(UITapGestureRecognizer *)tap
{
    [self createPickerView:2];
}
//日期设置
- (void)dateTapAction:(UITapGestureRecognizer *)tap
{
    [self createPickerView:3];
}
#pragma mark -
#pragma mark 创建pickerView
- (void)createPickerView:(NSInteger)tag
{
    self.picView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 200, self.view.frame.size.width, 200)];
    _picView.backgroundColor = [UIColor cyanColor];
    _picView.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self.imageView addSubview:_picView];
    UIPickerView *pick = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 0, _picView.frame.size.width - 100, _picView.frame.size.height - 50)];
    pick.tag = tag;
    pick.delegate = self;
    pick.dataSource = self;
    pick.showsSelectionIndicator = YES;
    [_picView addSubview:pick];
    [pick release];
    
    UIButton *confirm = [[UIButton alloc] initWithFrame:CGRectMake(0, pick.frame.size.height + pick.frame.origin.y, self.view.frame.size.width, 50)];
    [_picView addSubview:confirm];
    
    [confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirm setTitle:@"确定" forState:UIControlStateNormal];
    if (tag == 3) {
        [confirm addTarget:self action:@selector(dateAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    else if (tag == 2)
    {
        [confirm addTarget:self action:@selector(timeAction:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [confirm addTarget:self action:@selector(repeatAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    [confirm release];
    [_picView release];
}
#pragma mark -
#pragma mark 返回字符串赋给label
- (void)repeatAction:(UIButton *)button
{
    UILabel *label = (UILabel *)[self.view viewWithTag:100];
    label.text = self.repeatStr;
    [self.dic setObject:self.repeatStr forKey:@"repeat"];
    [_picView removeFromSuperview];
}
- (void)dateAction:(UIButton *)button
{
    self.dateStr = [NSString stringWithFormat:@"%@%@",self.monthStr,self.dayStr];
    UILabel *label = (UILabel *)[self.view viewWithTag:300];
    label.text = [NSString stringWithFormat:@"%@ 开始服药",self.dateStr];
    [_picView removeFromSuperview];
}

- (void)timeAction:(UIButton *)button
{
    self.timeStr = [NSString stringWithFormat:@"%@:%@",self.hourStr,self.minStr];
    UILabel *label = (UILabel *)[self.view viewWithTag:200];
    label.text = [NSString stringWithFormat:@"%@",self.timeStr];
    [self.dic setObject:self.timeStr forKey:@"time"];
    [_picView removeFromSuperview];
}


//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    
    
    if (pickerView.tag == 3) {
        return 3;
    }else if(pickerView.tag == 2)
    {
        return 2;
    }else
    {
        return 1;
    }
}

//行数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag == 3) {
        if (component == 0) {
            return self.yearArray.count;
        }else if (component == 1)
        {
            return self.monthArray.count;
        }else
        {
            return self.DaysArray.count;
        }
    }else if(pickerView.tag == 2)
    {
        if (component == 0) {
            return self.hourArray.count;
        }else
        {
            return self.minuteArray.count;
        }
    }else{
        return self.repeatArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.tag == 3) {
        if (component == 0) {
            return [self.yearArray objectAtIndex:row];
        }else if (component == 1)
        {
            return [self.monthArray objectAtIndex:row];
        }else
        {
            return [self.DaysArray objectAtIndex:row];
        }
    }else if(pickerView.tag == 2)
    {
        if (component == 0) {
            NSString *hour = [self.hourArray objectAtIndex:row];
            NSInteger hourstr = [hour integerValue];
            if (hourstr <= 9) {
                hour = [NSString stringWithFormat:@"0%@",hour];
            }
            return hour;
        }else
        {
            NSString *minute = [self.minuteArray objectAtIndex:row];
            NSInteger minutestr = [minute integerValue];
            if (minutestr <= 9) {
                minute = [NSString stringWithFormat:@"0%@",minute];
            }
            
            return minute;
        }
    }else{
        return [self.repeatArray objectAtIndex:row];
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == 3) {
        
        if (component == 1) {
            self.monthStr = [self.monthArray objectAtIndex:row];
        }if (component == 2) {
            self.dayStr = [self.DaysArray objectAtIndex:row];
        }
    }else if(pickerView.tag == 2)
    {
        if (component == 0) {
            NSString *hour = [self.hourArray objectAtIndex:row];
            NSInteger hourstr = [hour integerValue];
            if (hourstr <= 9) {
                hour = [NSString stringWithFormat:@"0%@",hour];
            }
            self.hourStr = hour;
        }else
        {
            NSString *minute = [self.minuteArray objectAtIndex:row];
            NSInteger minutestr = [minute integerValue];
            if (minutestr <= 9) {
                minute = [NSString stringWithFormat:@"0%@",minute];
            }
            self.minStr = minute;
        }
    }else
    {
        self.repeatStr = [self.repeatArray objectAtIndex:row];
    }
}

//键盘返回
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITextField *field1 = (UITextField *)[self.view viewWithTag:30];
    [field1 resignFirstResponder];
    UITextField *field2 = (UITextField *)[self.view viewWithTag:20];
    [field2 resignFirstResponder];
    UITextField *field3 = (UITextField *)[self.view viewWithTag:10];
    [field3 resignFirstResponder];
    [self.picView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
